'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 进入战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../consts/code');
var atkData = require('../../../libs/war/atkData');
var surviveData = require('../../../libs/war/surviveData');
var constantUtil = require('../../../util/constantUtil');
var PlayerManager = require('../../../libs/playerManager');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 进入副本协议
 * 要给客户端下发的数据:
 * 1.敌方的建筑
 * 2.敌方的英雄,小兵
 * 3.我方的英雄,小兵
 */
Handler.prototype.enter = function (msg, session, next) {
    var role_id = session.uid;
    var friend_role_id_list = msg.friend_role_id_list || [];

    var atk_data;
    var def_data;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'survive', 'build', 'task', 'build', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var survive_model = player_manager.survive;
                // todo: 暂时关闭
                // if (!!survive_model.isEnd()) {
                //     return next(null, {
                //         code: Code.SURVIVE_IS_END
                //     });
                // }

                var next_wave = survive_model.getCurrentWave() + 1;
                def_data = _getDef(next_wave);

                // 我方数据
                _getAtk(role_id, friend_role_id_list, cb);
            },
            function (data, cb) {
                atk_data = data;
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    atk: atk_data,
                    def: def_data
                }
            });
        }
    );
};

/**
 * 获取攻击方数据
 * @return {[type]} [description]
 */
var _getAtk = function (role_id, friend_role_id_list, cb) {
    var atk_data;
    var friend_list = [];
    async.waterfall(
        [
            function (cb) {
                atkData.getData(role_id, cb);
            },
            function (data, cb) {
                atk_data = data;

                var first_friend_role_id = friend_role_id_list[0];
                if (!!first_friend_role_id) {
                    atkData.getDataNoSoldier(first_friend_role_id, cb);
                } else {
                    cb(null, null);
                }
            },
            function (data, cb) {
                if (!!data) {
                    friend_list.push(data);
                }

                var second_friend_role_id = friend_role_id_list[1];
                if (!!second_friend_role_id) {
                    atkData.getDataNoSoldier(second_friend_role_id, cb);
                } else {
                    cb(null, null);
                }
            },
            function (data, cb) {
                if (!!data) {
                    friend_list.push(data);
                }
                cb();
            }
        ],
        function (err) {
            atk_data.friend_list = friend_list;
            cb(null, atk_data);
        }
    );
};

/**
 * 获取防守方数据
 * @return {[type]} [description]
 */
var _getDef = function (wave_num) {
    return surviveData.getData(wave_num);
};
